package fss.util.rmi;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;


/**
 * This program will execute <code>rmiregistry [port]</code> programmatically. 
 * <p>If no port is provided, the default 1099 is used.
 * 
 * @author Thinh Ho
 *
 */
public class StartRmiRegistry {
    private static final String USAGE = "StartRmiRegistry <port>";
    /**
     * @param args
     */
    public static void main(String[] args) {        
        if(args.length != 1) {
            System.out.println("Error: Invalid number of arguments. Expecting 1.");
            System.out.println("Usage: " + USAGE);
            System.exit(-1);
        }
        
        int port = Integer.parseInt(args[0]);
        
        try {
            System.out.println("Starting the registry on port " + port + "... ");
            LocateRegistry.createRegistry(port);
            LocateRegistry.getRegistry(port);
            System.out.println("Registry created on port " + port + ". Hit enter to stop registry. ");
            
            BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
            stdin.readLine();
            System.out.println("Registry stopped on port " + port + ".");
            
        } catch (RemoteException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
